﻿// <copyright file="HandleFrame.cs" company="Microsoft">
// Copyright (c) 2010 Microsoft All Rights Reserved
// </copyright>
// <author>Microsoft</author>
// <email>mischero@microsoft.com</email>
// <date>2010-01-01</date>
// <summary>Web analytics service behavior class definition</summary>

namespace Microsoft.WebAnalytics.Navigation
{
    using System;
    using System.ComponentModel.Composition;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Microsoft.WebAnalytics;
    using Microsoft.WebAnalytics.Contracts;

    /// <summary>
    /// Handle Frame events
    /// </summary>
    [Tracker(typeof(Frame))]
    public class HandleFrame : ITrackHandler
    {
        #region Properties
        /// <summary>
        /// Gets or sets the event Logging method
        /// </summary>
        [Import("Log")]
        public Action<AnalyticsEvent> Log { get; set; }
        #endregion

        #region ITrackAction Members

        /// <summary>
        /// Track the Frame Navigated and NavigationFailed events.
        /// </summary>
        /// <param name="associatedObject">the associated object</param>
        /// <param name="analyticsEvent">the analytics event</param>
        /// <param name="eventArgs">the event arguments</param>
        public void Track(
            System.Windows.FrameworkElement associatedObject, 
            AnalyticsEvent analyticsEvent, 
            System.EventArgs eventArgs)
        {
            var frame = associatedObject as Frame;

            switch (analyticsEvent.Name)
            {
                case "Navigated":
                    this.TrackNavigated(frame, analyticsEvent, eventArgs as NavigationEventArgs);
                    break;

                case "NavigationFailed":
                    this.TrackNavigationFailed(frame, analyticsEvent, eventArgs as NavigationFailedEventArgs);
                    break;
                default:
                    this.Log(analyticsEvent);
                    break;
            }
        }

        /// <summary>
        /// Track frame navigation failed events
        /// </summary>
        /// <param name="frame">the navigation frame</param>
        /// <param name="analyticsEvent">the analytics event</param>
        /// <param name="navigationFailedEventArgs">the navigation failed event arguments</param>
        private void TrackNavigationFailed(Frame frame, AnalyticsEvent analyticsEvent, NavigationFailedEventArgs navigationFailedEventArgs)
        {
            // Initialize the AnalyticsEvent with parameters specific to the control event
            analyticsEvent.Name = "NavigationFailed";
            analyticsEvent.ObjectName = frame.Name;
            analyticsEvent.ObjectType = typeof(Frame).Name;
            analyticsEvent.ActionValue = navigationFailedEventArgs.Uri.ToString();

            analyticsEvent.EventArgs.Add(new Microsoft.WebAnalytics.Data.PropertyValue { PropertyName = "ErrorMessage", Value = navigationFailedEventArgs.Exception.Message });

            // Log the event
            this.Log(analyticsEvent);
        }

        /// <summary>
        /// Track frame navigated events
        /// </summary>
        /// <param name="frame">the navigation frame</param>
        /// <param name="analyticsEvent">the analytics event</param>
        /// <param name="navigationEventArgs">the navigation event arguments</param>
        private void TrackNavigated(Frame frame, AnalyticsEvent analyticsEvent, NavigationEventArgs navigationEventArgs)
        {
            // Initialize the AnalyticsEvent with parameters specific to the control event
            analyticsEvent.Name = "Navigated";
            analyticsEvent.ObjectName = frame.Name;
            analyticsEvent.ObjectType = typeof(Frame).Name;
            analyticsEvent.ActionValue = navigationEventArgs.Uri.ToString();
            analyticsEvent.HitType = Data.HitType.PageView;

            // Log the event
            this.Log(analyticsEvent);
        }

        #endregion
    }
}
